<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="rules"
    label-width="100px"
    style="padding: 20px 40px"
  >
    <el-form-item label="表单名称" prop="name">
      <el-input v-model="formData.name" placeholder="请输入表单名称" />
    </el-form-item>
    <!-- 表单OID与版本号已移除 -->
    <el-form-item label="表单状态" prop="status">
      <el-select v-model="formData.status" placeholder="请选择表单状态" style="width: 100%">
        <el-option label="草稿" value="draft" />
        <el-option label="已发布" value="published" />
        <el-option label="已停用" value="disabled" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
      <el-button @click="handleCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref, defineProps, watch } from 'vue'

const props = defineProps({
  isEdit: {
    type: Boolean,
    default: false
  },
  editData: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['close'])

const formRef = ref(null)
const formData = ref({
  name: '',
  version: '1.0.0',
  status: 'draft'
})

const rules = {
  name: [{ required: true, message: '请输入表单名称', trigger: 'blur' }],
  status: [{ required: true, message: '请选择表单状态', trigger: 'change' }]
}

// 确认提交
const handleConfirm = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid) => {
    if (valid) {
      emit('close', formData.value)
    }
  })
}

// 取消
const handleCancel = () => {
  emit('close')
}

// 监听编辑数据变化
watch(
  () => props.editData,
  (newVal) => {
    if (props.isEdit && newVal) {
      formData.value = { ...newVal }
    }
  },
  { immediate: true }
)
</script> 